package www.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import www.dao.StudentDao;
import www.domain.Student;
import www.service.StudentService;


@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;


    @Override
    public Student findStudentById(int id) {
        return studentDao.findStudentById(id);
    }

    @Override
    public void transfer(String from, String to, int money) {
        //已经自带事务
            Student stu1 = new Student();
            stu1.setName(from);
            stu1.setMoney(-money);
            Student stu2 = new Student();
            stu2.setName(to);
            stu2.setMoney(money);

//            自定义
            String constant_ping = "ping12<>34an567(89)";

        if(studentDao.findStudentByName(from) != null && studentDao.findStudentByName(to)!= null){
            studentDao.updateStudentMoney(stu1);
            studentDao.updateStudentMoney(stu2);
        }
    }

    @Override
    public Student login(Student student) {
        Student stubyname = studentDao.findStudentByName(student.getName());
        if(stubyname != null){
            stubyname.setPassword(null);
        }


        if (stubyname != null){
            //用户名对了，才有可能进行用户名密码的校验操作
            Student stu1 = studentDao.login(student);

            //不能给一个null赋值东西
            if(stu1 == null){
                return stubyname;
            }else{
                return stu1;
            }

        }else {
            return null;
        }

    }
}
